/**
 * 
 */
package com.base.myproject.client.ui;

import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.tools.Debug;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ComponentManager;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.menu.CheckMenuItem;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Accessibility;

/**
 * @author zgw
 *2010-1-3
 */
public class ToggleButtonX  extends Button  implements  Listener{

	  private boolean pressed;
	  private String toggleGroup;
	  private boolean allowDepress = true;
	  private BaseStore<BusiObject> store = null;
	  private Html html =new Html();
	  private String[] strarray ;

	  /**
	   * Creates a new toggle button.
	   */
	  public ToggleButtonX() {
	    super();
	  }

	  /**
	   * Creates a new toggle button.
	   * 
	   * @param text the button text
	   */
	  public ToggleButtonX(String text) {
	    super(text);
	  }

	  /**
	   * Creates a new toggle button.
	   * 
	   * @param text the button text
	   * @param listener a selection listener
	   */
	  public ToggleButtonX(String text, SelectionListener<ButtonEvent> listener) {
	    super(text, listener);
	  }

	  /**
	   * Creates a new toggle button with the given text and icon.
	   * 
	   * @param text the button text
	   * @param icon the icon
	   */
	  public ToggleButtonX(String text, AbstractImagePrototype icon) {
	    this(text);
	    setIcon(icon);
	  }
	  
	  
	  public ToggleButtonX(String text,String[] strarray) {
		    this(text);
		    Menu menu = new Menu();  
		    Debug.printf("--");
		    for(int i=0;i<strarray.length;i++)
		    {
		    	 CheckMenuItem r = new CheckMenuItem(strarray[i]);  
		    	//Radio r = new Radio();
				//r.setBoxLabel(strarray[i]);
		    	 r.setGroup("radios");  
		    	
		    	 r.addSelectionListener(new SelectionListener<MenuEvent>(){

					@Override
					public void componentSelected(MenuEvent ce) {
						System.out.println("++"+((CheckMenuItem)ce.getItem()).getText());
						
						toggle(true,true);
					}
		    		 
		    	 });
				menu.add(r);
		    }
		    CheckMenuItem r = new CheckMenuItem("取消选择"); 
		    r.setGroup("radios");
		    menu.add(r);
		    
		    r.addListener(Events.CheckChange, this);
		    this.setMenu(menu);
		  }

	  /**
	   * 获得按钮的选择项，无选择返回null
	   * @return
	   */
	  public String getvalueitem()
	  {
		  for(int i=0;i<menu.getItemCount();i++)
		  {
			  if(menu.getItem(i) instanceof CheckMenuItem)
			  {
				  if(((CheckMenuItem)menu.getItem(i)).isChecked())
				  {
					  return ((CheckMenuItem)menu.getItem(i)).getText();
				  }
			  }
		  }
		  return null;
	  }
	  /**
	   * Returns the toggle group name.
	   * 
	   * @return the toggle group name
	   */
	  public String getToggleGroup() {
	    return toggleGroup;
	  }

	  public boolean isAllowDepress() {
	    return allowDepress;
	  }

	  /**
	   * Returns true if the button is pressed.
	   * 
	   * @return the pressed state
	   */
	  public boolean isPressed() {
	    return pressed;
	  }

	  /**
	   * Toggles the current state.
	   */
	  public void toggle() {
	    toggle(!pressed, false);
	  }
	  
	  /**
	   * Sets the current pressed state.
	   * 
	   * @param state true to set pressed state
	   */
	  public void toggle(boolean state) {
	    toggle(state, false);
	  }

	  /**
	   * True to allow a toggle item to be depressed (default to true).
	   * 
	   * @param allowDepress true to allow depressing
	   */
	  public void setAllowDepress(boolean allowDepress) {
	    this.allowDepress = allowDepress;
	  }

	  /**
	   * Optionally, set the the button's toggle group name.
	   * 
	   * @param toggleGroup the toggle group name
	   */
	  public void setToggleGroup(String toggleGroup) {
	    this.toggleGroup = toggleGroup;
	  }

	  @Override
	  protected void onClick(ComponentEvent ce) {
	    ce.stopEvent();
	    focus();
	    hideToolTip();
	    if (!disabled) {
//	      if (allowDepress || !pressed) {
//	        toggle();
//	      }
	      if (menu != null && !menu.isVisible()) {
	        showMenu();
	      }
	      ButtonEvent be = new ButtonEvent(this);
	      be.setEvent(ce.getEvent());
	      fireEvent(Events.Select, be);
	    }
	  }

	  @Override
	  protected void onFocus(ComponentEvent ce) {
	    if (!pressed) {
	      super.onFocus(ce);
	    }
	  }

	  @Override
	  protected void onMouseDown(ComponentEvent ce) {
	    // do nothing
	  }

	  @Override
	  protected void onMouseOver(ComponentEvent ce) {
	    if (!pressed) {
	      super.onMouseOver(ce);
	    }
	  }

	  @Override
	  protected void onMouseUp(ComponentEvent ce) {
	    // do nothing
	  }

	  @Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos);
	    if (pressed) {
	      toggle(pressed, true);
	    }
	  }

	  /**
	   * Sets the current pressed state.
	   * 
	   * @param state true to set pressed state
	   * @param silent true to not fire the toggle event
	   */
	  protected void toggle(boolean state, boolean silent) {
	    this.pressed = state;
	    if (rendered) {
	      ButtonEvent be = new ButtonEvent(this);
	      el().setStyleName("x-btn-pressed", state);
	      if (state) {
	        onBlur(null);
	        removeStyleName(baseStyle + "-over");
	      } else {
	        onFocus(null);
	      }
	      if (state && toggleGroup != null && toggleGroup.length() > 0) {
	        List<ToggleButtonX> list = ComponentManager.get().get(ToggleButtonX.class);
	        for (ToggleButtonX tb : list) {
	          if (tb != this && tb.getToggleGroup() != null && tb.getToggleGroup().equals(toggleGroup)) {
	            tb.toggle(false, silent);
	          }
	        }
	      }
	      Accessibility.setState(buttonEl.dom, "aria-pressed", "" + state);
	      if (!silent) {
	        fireEvent(Events.Toggle, be);
	      }
	    }
	  }

	@Override
	public void handleEvent(BaseEvent be) {
		if(be.getType().equals(Events.CheckChange))
		{
			toggle(false, false);
		}
	}

	public BaseStore<BusiObject> getStore() {
		return store;
	}

	public void setStore(final BaseStore<BusiObject> store) {
		this.store = store;
		
		
		this.store.addBaseStoreListener(BaseStoreEvent.EventType.LOAD_COMPLETE,
				new BaseStoreListener() {

					@Override
					public void BaseStoreEventHandle(BaseStoreEvent evt) {

						//formpanel.mask("loading");
						if (store.getCount() > 0)
							{
							//formbinding.autoBind();
							//formbinding.bind(store.getAt(0));
							html.setHtml(""+store.getAt(0).get("serviceitem"));
							}
						else {
							//formbinding.unbind();
						}
						// formpanel.el().mask().hide();
						//formpanel.unmask();

					}

					@Override
					public void onBrowserEvent(Event event) {
						// TODO Auto-generated method stub

					}

				});
//		basestore.addBaseStoreListener(BaseStoreEvent.EventType.CELL_CHANGED,
//				new BaseStoreListener() {
//
//					@Override
//					public void BaseStoreEventHandle(BaseStoreEvent evt) {
//						// formpanel.el().mask("读取数据中...").show();
//						
//						formbinding.bind(basestore.getAt(evt.getCurrowindex()));
//						// formpanel.el().mask("读取数据中...").hide();
//
//					}
//
//					@Override
//					public void onBrowserEvent(Event event) {
//						// TODO Auto-generated method stub
//
//					}
//
//				});
		this.store.addBaseStoreListener(BaseStoreEvent.EventType.ROW_CHANGED,
				new BaseStoreListener() {

					@Override
					public void BaseStoreEventHandle(BaseStoreEvent evt) {
						//formbinding.bind(basestore.getAt(evt.getCurrowindex()));
						
						html.setHtml(""+store.getAt(evt.getCurrowindex()).get("serviceitem"));

					}

					@Override
					public void onBrowserEvent(Event event) {
						// TODO Auto-generated method stub

					}

				});
		
		
	}

	public String[] getStrarray() {
		return strarray;
	}

	public void setStrarray(String[] strarray) {
		this.strarray = strarray;
	}
	
	
	}